package com.Mt.Controller;

import com.Mt.Dto.Dto;
import com.Mt.Service.Mt_CommodityPurchaseService;
import com.Mt.Service.Mt_CommodityService;
import com.Mt.Utils.DtoUtil;
import com.Mt.mapper.Mt_CommodityPurchaseMapper;
import com.Mt.pojo.MtCommodityPurchase;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("comPurchase")
@RestController
public class Mt_CommodityPurchaseController {

    @Reference
    private Mt_CommodityPurchaseService mtCommodityPurchaseService;

    /**
     * 根据数量和商品id 来减少库存
     * @return
     */
    @GetMapping("/upd")
    @ResponseBody
    public Dto upd(){

        List<MtCommodityPurchase> list = mtCommodityPurchaseService.selMark();
        int i = 0;
        int o = 0;
        for (MtCommodityPurchase l:list) {
            int row = mtCommodityPurchaseService.upd(l.getNum(), l.getCommodity_id());
            if (row > 0) {
                o++;
                i = list.size();

                int rowDel = mtCommodityPurchaseService.delId(l.getCommodity_id());
                if(rowDel>0){
                    System.out.println("删除成功");
                }else{
                    System.out.println("删除失败");
                }

            } else {
                System.out.println("修改失败");
            }

        }
        if(i == o){

            return DtoUtil.returnSuccessData(list);

        }else{
            return DtoUtil.returnError(5001,"修改错误");
        }
    }

    @GetMapping("ins/{id}/{num}")
    public Dto ins(@PathVariable int id,@PathVariable int num){
        int row = mtCommodityPurchaseService.ins(id,num);
        if(row>0){
            return DtoUtil.returnSuccess();
        }else{
            return DtoUtil.returnError(2001,"参数错误");
        }


    }

    /**
     * 删除mark表所有数据
     * @return
     */
    @GetMapping("delALL")
    public Dto delALL(){
        int row = mtCommodityPurchaseService.delAll();
        if(row>0){
            return DtoUtil.returnSuccess();
        }else{
            return DtoUtil.returnError(2022,"删除失败");
        }

    }

}
